'use strict';

const Controller = require('egg').Controller;

/**
 * 200=成功
 * 301-399=错误码 
 */
class AuthController extends Controller {
    /**
     * 微信登录
     * @returns 成功返回token
     */
    async wxlogin() {
        const { ctx } = this;
        let { code, appId, appSecret } = ctx.request.body;
        let rs = await ctx.service.wechat.getopenid(code, appId, appSecret);
        rs = JSON.parse(rs);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs
        };
    }

    /**
     * 手机号码登录
     * @returns 成功返回token
     */
    async loginPhone() {
        const { ctx, app } = this;
        let { phone, smscode, codekey } = ctx.request.body;
        smscode = smscode || "smscode";
        let smscode_set = await ctx.service.redis.get(codekey) || "smscode_set";
        if ((smscode.toUpperCase()) != (smscode_set.toUpperCase())) {
            await app.utils.error.throw("短信验证码错误", -99);
        }
        //清空
        await ctx.service.redis.del(codekey);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {}
        };
    }

    /**
         * 微信登录
         * @returns 成功返回token
         */
    async getAccessToken() {
        const { ctx } = this;
        let { appId, appSecret } = ctx.request.body;
        let rs = await ctx.service.wechat.getAccessToken(appId, appSecret);
        console.log("rs",rs);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {}
        };
    }

}

module.exports = AuthController;